import { Injectable } from '@nestjs/common';
import { CreateBannerDto } from './dto/create-banner.dto';
import { UpdateBannerDto } from './dto/update-banner.dto';
import { Banner } from './entities/banner.entity';

@Injectable()
export class BannerService {
  create(createBannerDto: CreateBannerDto) {
    return Banner.save({
      ...createBannerDto,
      isShow: true,
      create_time: new Date(),
      update_time: new Date(),
      delete_time: null,
    });
  }

  async findAll(pageNum: number, pageSize: number) {
    const [list, count] = await Banner.findAndCount({
      skip: (pageNum - 1) * pageSize,
      take: pageSize,
    });

    return { list, count };
  }

  findOne(id: number) {
    return Banner.findOneBy({ id });
  }

  update(id: number, updateBannerDto: UpdateBannerDto) {
    return Banner.update(id, { ...updateBannerDto });
  }

  remove(id: number) {
    return Banner.update(id, { delete_time: new Date() });
  }
}
